using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace LenninSoftware.FormEditor.Adapters
{
    class DateTime : BaseControlAdapter
    {

        private int _localeID;

        public int LocaleID
        {
            get { return _localeID; }
            set { _localeID = value; }
        }

        public DateTime(int localeID)
        {
            _localeID = localeID;
        }

        public override System.Web.UI.Control CreateControl(FieldInfo field)
        {
            SPFieldInfo spField = (SPFieldInfo)field;
            SPFieldDateTime fieldInfo = (SPFieldDateTime)spField.InternalField;            

            DateTimeControl dateTime = new DateTimeControl();
            dateTime.DateOnly = fieldInfo.DisplayFormat == SPDateTimeFieldFormatType.DateOnly;            

            
            //dateTime.IsRequiredField = true;
            dateTime.LocaleId = LocaleID;
            return dateTime;
        }

        public override void LoadData(System.Web.UI.Control control, SPListItem item, object data)
        {
            DateTimeControl dateTime = (DateTimeControl)control;
            dateTime.SelectedDate = (System.DateTime)data;
        }

        public override object SaveData(System.Web.UI.Control control, SPListItem item, object currentData)
        {
            DateTimeControl dateTime = (DateTimeControl)control;
            return dateTime.SelectedDate;
        }

        public override void Reset(System.Web.UI.Control control)
        {
            DateTimeControl dateTime = (DateTimeControl)control;
            dateTime.ClearSelection();            
        }

        public override bool isEmpty(Control control)
        {
            DateTimeControl dateTime = (DateTimeControl)control;
            return dateTime.IsDateEmpty;
        }

        public override Control CreateReadOnlyControl(FieldInfo field)
        {
            Label label = new Label();            
            return label;
        }

        public override void LoadReadOnlyData(Control control, SPListItem item, object data)
        {
            Label label = (Label)control;
            label.Text = ((System.DateTime)data).ToString();
        }
    }
}
